home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Applications / PICSee Dust 1.01 / Secondary Source / PICS_Merge.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-15  |  4.6 KB  |  173 lines  |  [TEXT/CWIE]

  1. #include "PICS_Merge.h"
  2. #include "PICS_Types.h"                // Creator, file types
  3. #include "PICS_Info.h"                // Handle PICS' "INFO" resource
  4. #include "PICS_Utils.h"
  5.  
  6. #include "PICS_Operations.h"
  7. #include "Class_ProgressWindow.h"
  8. #include "FileRegistry.h"
  9. #include "ReadPicture.h"            // To read in PICT files
  10. #include "Select Multiple Files.h"    // To add PICT files in any order
  11. #include "StringUtils.h"            // Append, manipulate filenames
  12. #include "Monitors.h"
  13.  
  14. #include "main.h"                    // For AppEmergencyUpdate()
  15.  
  16. #include "SimpleError.h"            // For error-handling
  17. #include "assert_mac.h"
  18.  
  19. // ---------------------------------------------------------------------------
  20.  
  21. void SetupMergePICTFiles(short numFiles, FSSpec *pictFile) {
  22.     FSSpec *fileList;
  23.     OSType creatorTypePICS;
  24.  
  25.     creatorTypePICS = kPICSiliciousCreatorType;
  26.  
  27.     if (numFiles == 0) {
  28.         // We select file to merge via a customized get file dialog box...
  29.         // When done, SelectPICTFilesToMerge will have allocated an array
  30.         // of FSSpec's for us. It is up to the caller to dispose of this
  31.         // array...
  32.         SelectPICTFilesToMerge(&numFiles, &fileList);
  33.         AppEmergencyUpdate();
  34.  
  35.         if (numFiles > 1) {
  36.             MergePICTFiles(numFiles, fileList,
  37.                 creatorTypePICS, "\pMerged PICS");
  38.             DisposePtr((Ptr)fileList);
  39.         }
  40.         else if (numFiles == 1) {
  41.             SimpleError(kAlertErrID, kErrMsgID, kNeedMoreFilesErrMsg);
  42.             return;
  43.         }
  44.     }
  45.     else {
  46.         // We got our files via drag and drop.
  47.         fileList = pictFile;
  48.  
  49.         // Check all files to make sure they're PICT files
  50.         FInfo fileInfo;
  51.         OSErr myErr;
  52.         for (short i = 0; i < numFiles; i++) {
  53.             myErr = FSpGetFInfo(&fileList[i], &fileInfo);
  54.             if (myErr == noErr && fileInfo.fdType != kPICTFileType) {
  55.                 SimpleError(kAlertErrID, kErrMsgID, kDontAcceptPICSFileErrMsg);
  56.                 return;
  57.             }
  58.         }
  59.  
  60.         if (numFiles == 1) {
  61.             SimpleError(kAlertErrID, kErrMsgID, kNeedMoreFilesErrMsg);
  62.             return;
  63.         }
  64.  
  65.         MergePICTFiles(numFiles, fileList,
  66.             creatorTypePICS, "\pMerged PICS");
  67.     }
  68. } // END SetupMergePICTFiles
  69.  
  70. // ---------------------------------------------------------------------------
  71.  
  72. void MergePICTFiles(
  73.     short    numFiles,
  74.     FSSpec    *fileList,
  75.     OSType    creator,
  76.     Str31    fileName)  {
  77.  
  78.     StandardFileReply reply;
  79.     OSErr myErr;
  80.     short fileRefNum;
  81.     short oldRefNum;
  82.     
  83.     // Put up the standard save dialog
  84.     StandardPutFile("\pSave merged PICS into:", fileName, &reply);
  85.  
  86.     if (reply.sfGood)  {
  87.         if (reply.sfReplacing) {
  88.             myErr = FSpDelete(&reply.sfFile);
  89.             if (myErr != noErr) {
  90.                 return;
  91.             }
  92.         }
  93.  
  94.         AppEmergencyUpdate();
  95.         oldRefNum = CurResFile();
  96.  
  97.         // Create the file.
  98.         FSpCreateResFile(&reply.sfFile, creator, kPICSFileType1, reply.sfScript);
  99.         if (ResError() != noErr)  {
  100.             return;
  101.         }
  102.  
  103.         fileRefNum = FSpOpenResFile(&reply.sfFile, fsRdWrPerm);
  104.         if (fileRefNum != -1 || !RegisterFile(fileRefNum)) {
  105.             short picDepth = 0;
  106.             long picSize, largestPicSize;
  107.             PicHandle picHandle;
  108.  
  109.             ProgressWindowPtr progressWindow = new ProgressWindow(GetDeepestDevice(), numFiles);
  110.             SetPort(progressWindow->GetWindow());
  111.             TextFont(geneva);
  112.             TextSize(9);
  113.             TextFace(bold);
  114.             progressWindow->SetPrimaryMessage("\pMerging PICT file:");
  115.             
  116.             UseResFile(fileRefNum);
  117.  
  118.             SetCursor(*GetCursor(watchCursor));
  119.             largestPicSize = 0;
  120.             for (short i = 0, idStart = kPICSRsrcStartID; i < numFiles; i++, idStart++) {
  121.                 progressWindow->SetSecondaryMessage(fileList[i].name);
  122.                 progressWindow->Increment();
  123.  
  124.                 picHandle = ReadPicture(&fileList[i]);
  125.  
  126.                 if (picHandle != NULL) {
  127.                     if (i == 1)    // We need to get depth only once
  128.                         picDepth = GetPictDepth(picHandle);
  129.  
  130.                     picSize = GetHandleSize((Handle)picHandle);
  131.                     if (picSize > largestPicSize)
  132.                         largestPicSize = picSize;
  133.  
  134.                     UseResFile(fileRefNum);
  135.                     AddResource((Handle)picHandle, kPICSRsrcType, idStart, "\p");
  136.                     WriteResource((Handle)picHandle);
  137.                     ReleaseResource((Handle)picHandle);
  138.                 }
  139.                 else {
  140.                     delete progressWindow;
  141.                     UseResFile(oldRefNum);
  142.                     SimpleError(kAlertErrID, kErrMsgID, kUnableLoadPICTFileErrMsg);
  143.                     CloseResFile(fileRefNum);
  144.                     return;
  145.                 }
  146.             }
  147.             progressWindow->SetPrimaryMessage("\pDone.");
  148.             progressWindow->FinishProgress();
  149.             delete progressWindow;
  150.             
  151.             PICSInfoRsrcHdl picsInfo;
  152.             picsInfo = NewPICSInfoResource();
  153.             ASSERT(picsInfo != NULL);
  154.             if (picsInfo != NULL) {
  155.                 (**picsInfo).speed = kDefaultPICSSpeed;
  156.                 (**picsInfo).creatorType = kPICSiliciousCreatorType;
  157.                 (**picsInfo).largestFrameSize = largestPicSize;
  158.                 (**picsInfo).depth = picDepth;
  159.  
  160.                 if (!SavePICSInfo(picsInfo, true)) {
  161.                     // ERROR HANDLING
  162.                 }
  163.                 DisposePICSInfo(picsInfo);
  164.             }
  165.  
  166.             (void)UnregisterFile(fileRefNum);
  167.             CloseResFile(fileRefNum);
  168.             UseResFile(oldRefNum);
  169.             SetCursor(&qd.arrow);
  170.         }
  171.     }
  172. } // END MergePICTFiles
  173.